
var currency = '-';
var exchange = 1.0;
var exchange_item = 1.0;
var grid;

$(function() {
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "invoice",
	    name: "开票日期",
	    field: "invoice",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Date
	}, {
	    id: "name",
	    name: "事由",
	    field: "name",
	    width: 150,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "currency",
	    name: "货币",
	    field: "currency",
	    width: 80,
	    cssClass: "cell-title",
	    editor: CurrencySelectCellEditor
	}, {
	    id: "total",
	    name: "应收",
	    field: "total",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "content",
	    name: "备注",
	    field: "content",
	    width: 160,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "ok",
	    name: "",
	    field: "ok",
	    width: 40,
	    cssClass: "cell-effort-driven",	    
	    cannotTriggerInsert: true,
	    formatter: Slick.Formatters.YesNo,
	    editor: Slick.Editors.Checkbox
	}];

	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};
		
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
        var item = {date: "0000-00-00", total:"0.00", ok: true};
        
        if (currency != null && currency != '-') {
	    	item.currency = currency;
	    }    
	    
        $.extend(item, args.item);
	    data.push(item);
	    grid.invalidateRows([data.length - 1]);
	    grid.updateRowCount();
	    grid.render();
        
        total();
	});
    
    grid.onCellChange.subscribe(function(e, args) {
    	
    	item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
    	
    	if (item.currency == null || item.currency == '' || item.currency == '-') {
			item.currency = $("#currency").val();
		}
    	
    	// grid.removeRows([currentRow]);
    	grid.updateRowCount();
    	grid.render();
    	
    	total();
    });
});	
